package com.miyake.loveviewweb.client.viewer.table;

import java.util.List;

import com.google.gwt.user.cellview.client.DataGrid;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.miyake.loveviewweb.client.DataServiceWrapper;
import com.miyake.loveviewweb.shared.model.DataColumnModel;
import com.miyake.loveviewweb.shared.model.DataModel;

public class DataGridTable extends Composite implements ITable {
    DataGrid<String[]> dataGrid = new DataGrid<String[]>();
    private DataModel dataModel;
    
    public DataGridTable(DataModel dataModel) {
        this.dataModel = dataModel;
        initialize(dataModel);
    }

    private void initialize(DataModel dataModel) {
        initWidget(dataGrid);
        dataGrid.setWidth("100%");
        dataGrid.setHeight("150px");

       // dataGrid.setHeight("1px");
        abstract class TextColumnEx<T> extends TextColumn<T>{
            public TextColumnEx(int number) {
                this.number = number;
            }
            int number;
        }
        for (int i = 0; i < dataModel.getXyDataBody().getDataColumn().size(); i++) {
            dataGrid.addColumn(new TextColumnEx<String[]>(i) {  
                @Override
                public String getValue(String[] object) {
                    return object[number];
                }     
            }, dataModel.getXyDataBody().getDataColumn().get(i).getHeader());    
        }
        
        if (!dataModel.isContent()) {
            DataServiceWrapper.getInstance().getService().getData(dataModel.getKey(), true, new AsyncCallback<DataModel>() {
                public void onFailure(Throwable caught) {
                }

                public void onSuccess(DataModel result) {
                    dataGrid.setRowData(new TableDataSwapper(result.getXyDataBody().getDataColumn()).get());
                }
            });
        }
        else {
            dataGrid.setRowData(new TableDataSwapper(dataModel.getXyDataBody().getDataColumn()).get());
        }
    }

    public Widget getUiWidget() {
        return this;
    }

    public List<DataColumnModel> getDataColumnList() {
        return dataModel.getXyDataBody().getDataColumn();
    }
 
    

}
